package cn.wolfcode.service.impl;

import cn.wolfcode.entity.HisNew;
import cn.wolfcode.entity.News;
import cn.wolfcode.mapper.HisNewMapper;
import cn.wolfcode.mapper.NewsMapper;
import cn.wolfcode.qo.NewsQuery;
import cn.wolfcode.service.INewsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dou
 * @since 2023-04-13
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {
@Autowired
private HisNewMapper hisNewMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public Page<News> queryPage(NewsQuery qo) {
        Page<News> pageParam =new Page<>();
        pageParam.setCurrent(qo.getCurrent());
        pageParam.setSize(qo.getSize());
        QueryWrapper<News> qw=new QueryWrapper<>();
        return newsMapper.selectPage(pageParam,qw);
    }

    @Override
    public void updateViewNum(long id, long viewnum) {
        UpdateWrapper<News> uw=new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("viewnum",viewnum);
        newsMapper.update(null,uw);
    }

    @Override
    public void insertnow22(News news1) {
        newsMapper.insertnow22(news1);
    }

    @Override
    public List<News> getByHisId(Long hisId) {
        //通过hisid找出newsid
        QueryWrapper<HisNew> qw = new QueryWrapper<>();
        qw.eq("his_id",hisId);
        List<HisNew> hisNews = hisNewMapper.selectList(qw);
        ArrayList<News> news1 = new ArrayList<>();
        for (HisNew hisNew : hisNews) {
            News news = newsMapper.selectById(hisNew.getNewId());
            news1.add(news);
        }
        return news1;
    }
}
